package com.addressbook.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.addressbook.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase {
	
	private SortedListOf<ContactData> cacheContacts;

	public static boolean CREATION = true;
	public static boolean EDITION = false;
	
	public ContactHelper(ApplicationManager app) {
		super(app);
	}

	public ContactHelper fillContact(ContactData contact, boolean formType) {
		type("firstname", contact.getName());
		type("lastname", contact.getLastName());
		type("address", contact.getAddress());
		type("home", contact.getHome());
		type("mobile", contact.getMobile());
		type("work", contact.getWork());
		type("email", contact.getEmail());
		type("email2", contact.getEmail2());
		selectByText("bday", contact.getBirthDay());
		selectByText("bmonth", contact.getBirthMonth());
		type("byear", contact.getBirthYear());
		if (formType == CREATION){
			//selectByText("new_group", contact.getGroup());
		} else if (driver.findElements(By.name("new_group")).size() !=0){
			throw new Error("Group selector exists in contact modification form");
		}
		// 
		type("address2", contact.getAddress2());
		type("phone2", contact.getHome2());
		return this;
	}

	public ContactHelper submitAddContact() {
		click(By.name("submit"));
		cacheContacts = null;
		return this;
	}

	public ContactHelper selectContactByIndex(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));
		return this;
	}

	/*
	 * public void selectGroupByIndex(int index) {
	 * click(By.xpath("//select[@name='new_group']/option[" + (index-1) + "]"));
	 * }
	 */

	public ContactHelper submitDeleteContact() {
		click(By.xpath("//input[@value='Delete']"));
		cacheContacts = null;
		return this;
	}

	public ContactHelper submitEditContact() {
		click(By.xpath("//input[@value='Update']"));
		cacheContacts = null;
		return this;
	}

	public SortedListOf<ContactData> getContacts() {
		if (cacheContacts == null){
			rebuildcache();
		}
		return cacheContacts;
	}

	private void rebuildcache() {
		cacheContacts = new SortedListOf<ContactData>();

		manager.navigateTo().ContactList();
		List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@name='selected[]']"));
		List<WebElement> firstnames = driver.findElements(By.xpath("//tbody//td[2]"));
		List<WebElement> lastnames = driver.findElements(By.xpath("//tbody//td[3]"));
		List<WebElement> homenumber = driver.findElements(By.xpath("//tbody//td[5]"));
		for (int index = 0; index < checkboxes.size(); index++) {
			ContactData contact = new ContactData()
				.withId(checkboxes.get(index).getAttribute("value"))
				.withLastName(lastnames.get(index).getText())
				.withName(firstnames.get(index).getText());
			String str = checkboxes.get(index).getAttribute("accept");			
			int ind = str.indexOf(';');			
			contact.withEmail(str.substring(0, ind!=-1?ind:str.length()))
				.withEmail2("");
			if (ind != -1) {
				String email2 = str.substring(ind + 1);
				contact.withEmail2(email2);
			}
			contact.withHome(homenumber.get(index).getText());
			cacheContacts.add(contact);
		}		
	}

	public ContactHelper createContract(ContactData contact) {
		manager.navigateTo().AddNewContact();
		fillContact(contact, CREATION);
		submitAddContact();
		rebuildcache();
		return this;		
	}

	public ContactHelper deleteContactByindex(int index) {
		manager.navigateTo().ContactList();
		selectContactByIndex(index);
		submitDeleteContact();
		rebuildcache();
		return this;
	}

	public ContactHelper editContract(int index, ContactData contact) {
		manager.navigateTo().ContactList();
		selectContactByIndex(index);
		fillContact(contact, EDITION);
		submitEditContact();
		rebuildcache();
	    return this;
	}
}